package com.yuhuan.leetcode;

/**
 * <p>
 * Title:com.yuhuan.leetcode
 * </p>
 * <p/>
 * <p>
 * Description: 描述【】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2018
 * </p>
 * <p/>
 * <p>
 * School: 太原工业学院
 * </p>
 *
 * @author GuoYuhuan
 * @version 1.0
 * @created 2020/2/11 21:08
 */
public class LeetCode1 {
    public static void main(String[] args) {
 int []num = new int[]{1,2,2,3,3,3,4,5};
 int []num1 = new int[]{8,8,7,7,7};
        System.out.println(LeetCode2.majorityElement(num1));

//        System.out.println(removeDuplicates(num));
//        for(int i = 0; i< num.length;i++){
//            System.out.print(num[1]+" ");
//        }
    }

    public static int removeDuplicates(int[] nums) {
        int count=nums.length;
        int pre = 0;
        for (int i = 0 ;i < nums.length  ; i++){
            if (nums[pre] != nums[i] && i-pre>1){
                nums[pre+1] = nums[i];
                pre++;

            }
            if(nums[pre] == nums[i] ) {
                count--;
                pre++;
            }
        }
        return  count;
    }
}

class LeetCode2{
    /**
     * 我做的lj
     * @param nums
     * @return
     */
    public static  int majorityElement(int[] nums) {
        int count = 0;
        int num = 0;
        int result = 0 ;
        while (count<=nums.length/2&&num<=nums.length-1){
            int temp = nums[num];
            for(int i=num;i<nums.length;i++){
                if(nums[i]==temp){
                    count++;
                    result = nums[i];
                }
            }
            if(count<=nums.length/2){count = 0;}
            num++;
        }
        return result;
    }

    /**
     * 官方正解
     * @param nums
     * @return
     */
    public int majorityElementGuanFang(int[] nums) {
        int majorityCount = nums.length/2;
        for (int num : nums) {
            int count = 0;
            for (int elem : nums) {
                if (elem == num) {
                    count += 1;
                }
            }
            if (count > majorityCount) {
                return num;
            }

        }

        return -1;
    }



}